package com.ba.grain;

import java.util.ArrayList;

import util.FoodType;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class DataUploadAdapter extends BaseAdapter {

	private Context mContext;
	private LayoutInflater mLayoutInflater;

	public DataUploadAdapter(Context context) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(mContext);
	}

	final ArrayList<ListItem> mItems = new ArrayList<ListItem>();

	@Override
	public int getCount() {
		return mItems.size();
	}

	@Override
	public Object getItem(int index) {
		return mItems.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.data_upload_item, parent, false);
		}
		TextView address = (TextView) convertView.findViewById(R.id.tv_collect_address);
		TextView uploaded = (TextView) convertView.findViewById(R.id.tv_upload_statu);
		TextView buyPrice = (TextView) convertView.findViewById(R.id.tv_buy_price);
		TextView tradePrice = (TextView) convertView.findViewById(R.id.tv_trade_price);
		TextView dayRetailPrice = (TextView) convertView.findViewById(R.id.tv_day_retail_price);
		TextView weekRetailPrice = (TextView) convertView.findViewById(R.id.tv_week_retail_price);
		ListItem item = (ListItem) getItem(position);
		FoodType ft = item.getFood();
		address.setText(ft != null ? ft.name : "未知粮品");
		uploaded.setText(!item.isUploaded() ? "未上报" : "已上报");
		buyPrice.setText(String.format("%s：%s", "购买价", String.format("%1$.2f", item.buyPrice)));
		tradePrice.setText(String.format("%s：%s", "交易价", String.format("%1$.2f", item.tradePrice)));
		dayRetailPrice.setText(String.format("%s：%s", "日批发价",
				String.format("%1$.2f", item.dayRetailPrice)));
		weekRetailPrice.setText(String.format("%s：%s", "周批发价",
				String.format("%1$.2f", item.weekRetailPrice)));
		
		if (mContext instanceof ReportActivity) {
			View date_time = convertView.findViewById(R.id.date_time);
			date_time.setVisibility(View.VISIBLE);
			TextView tvDateTime = (TextView) date_time.findViewById(R.id.tv_collect_date_time);
			tvDateTime.setText(item.mSaveDateTime);
		}
		
		return convertView;
	}

	public void add(ListItem item) {
		mItems.add(item);
	}
}
